package com.fili;

import java.util.Map;
import java.util.zip.ZipOutputStream;
import java.util.zip.ZipEntry;
import java.io.*;

/**
 * Created by Claudiu-Florin FILIMON
 * Date: Oct 24, 2006
 * Time: 10:40:11 AM
 * Revision: 0
 */
public class PosterMain
{
    public String createZip(String jadName)
    {
        int p = jadName.toUpperCase().indexOf(".JAD");
        if (p < 0)
            return null;

        String zipName = jadName.substring(0, p) + ".zip";
        Map map = JadParser.parseJar(jadName);
        if (map == null)
            return null;

        String jarName = (String) map.get("MIDlet-Jar-URL");
        if (jarName == null)
            return null;

        try
        {
            File f = new File(jadName);
            FileInputStream fis = new FileInputStream(f);
            byte[] jadData = new byte[(int) f.length()];
            fis.read(jadData);

            f = new File(jarName);
            fis = new FileInputStream(f);
            byte[] jarData = new byte[(int) f.length()];
            fis.read(jarData);

            ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipName));
            zos.putNextEntry(new ZipEntry(jadName));
            zos.write(jadData);

            zos.putNextEntry(new ZipEntry(jarName));
            zos.write(jarData);
            zos.close();
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            return null;
        }
        catch (IOException e)
        {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            return null;
        }


        return zipName;
    }

    public static void main(String[] args)
    {
    }
}
